//
//  DFComposeViewController.swift
//  新浪微博
//
//  Created by dafei on 16/5/29.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit

class DFComposeViewController: UIViewController {
    
    @objc private func close() {
    
        dismissViewControllerAnimated(true, completion: nil)
   
    }
    
    @objc private func send() {
    
        print("发送")
    
    
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.yellowColor()
        setupUI()
        
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    private lazy var rightBarbuttonItem: UIBarButtonItem = {
        let btn = UIButton()
        btn.setTitle("发送", forState: .Normal)
        //点击事件
        btn.addTarget(self, action: #selector(DFComposeViewController.send), forControlEvents: .TouchUpInside)
        //设置文字颜色
        btn.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Disabled)
        //设置背景图片
        btn.setBackgroundImage(UIImage(named: "common_button_orange"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: .Disabled)
        btn.setBackgroundImage(UIImage(named:"common_button_orange_highlighted"), forState: .Highlighted)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        btn.frame = CGRect(x: 0, y: 0, width: 45, height: 30)
        let barButtonitem = UIBarButtonItem(customView: btn)
        
        return barButtonitem
    
    }()
    //文本输入视图 & 文本输入框
    private lazy var textView: UITextView = {
    
        let tv = UITextView()
        tv.font = UIFont.systemFontOfSize(18)
        tv.textColor = UIColor.darkGrayColor()
        //设置滚动视图键盘消失的方式
        tv.keyboardDismissMode = .OnDrag
        //打开视图滚动
        tv.alwaysBounceVertical = true
        tv.delegate = self
        return tv
    
    }()
    
    private lazy var placeHolderLabel: UILabel = UILabel(title: "请输入要输出的内容", font: 18, textColor: UIColor.lightGrayColor())
    private lazy var toolBar: UIToolbar = UIToolbar()
    
}

extension DFComposeViewController {

    private func setupUI() {
    
        setNavBar()
        setTextView()
        setToolBar()
        
    
    }
    
    //自定义底部工具条
    private func setToolBar() {
        self.view.addSubview(toolBar)
        //设置约束
        toolBar.snp_makeConstraints { (make) in
            make.left.right.bottom.equalTo(self.view)
            //工具条有默认高度
        }
        var items = [UIBarButtonItem]()
        //操作数据 添加UIBarButtonItem
        let itemSettings = [["imageName": "compose_toolbar_picture","actionName": "selectPicture"],
                            ["imageName": "compose_mentionbutton_background","actionName": "selectPicture"],
                            ["imageName": "compose_trendbutton_background","actionName": "selectPicture"],
                            ["imageName": "compose_emoticonbutton_background", "actionName": "selectEmoticon"],
                            ["imageName": "compose_add_background","actionName": "selectPicture"] ]
        for item in itemSettings {
        
            let imageName = item["imageName"]
            let action = item["actionName"]!
            //添加自定义的UIBarButtonItem
            let item = UIBarButtonItem(imageName: imageName, title: nil, target: self, action: Selector(action))
            items.append(item)
            
            //添加弹簧 FlexibleSpace 可伸缩弹簧 在toolbar 可以均匀分布视图
            let space = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, target: nil, action: nil)
            items.append(space)
        
        }
        //移除最后一个
        items.removeLast()
        //添加
        toolBar.items = items
    
    
    }
    
    
    private func setNavBar() {
        navigationItem.leftBarButtonItem = UIBarButtonItem( title: "关闭", target: self, action: #selector(DFComposeViewController.close))
        navigationItem.rightBarButtonItem = rightBarbuttonItem
        //设置右边按钮不可交互
        navigationItem.rightBarButtonItem?.enabled = false
        
        //设置导航条自定义视图 是nil 不能直接添加视图
        let myview = UIView(frame: CGRect(x: 0, y: 0, width: 220, height: 40))
        let titleLabel = UILabel(title: "发布微博", font: 16, textColor: UIColor.darkGrayColor())
        let nameLabel = UILabel(title: DFUserAccountViewModel.sharedAccountViewModel.userAccount?.name ?? "", font: 12, textColor: UIColor.darkGrayColor())
        myview.addSubview(titleLabel)
        myview.addSubview(nameLabel)
        titleLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(myview.snp_centerX)
            make.top.equalTo(myview.snp_top)
            
        }
        nameLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(myview.snp_centerX)
            make.bottom.equalTo(myview.snp_bottom)
            
        }
        //赋值的操作
        navigationItem.titleView = myview
    
    }
    
    
    ///设置文本输入视图
    private func setTextView() {
    
        view.addSubview(textView)
        textView.snp_makeConstraints { (make) in
            make.top.left.right.equalTo(self.view)
            make.height.equalTo(ScreenHeight / 3)
        }
        textView.addSubview(placeHolderLabel)
        placeHolderLabel.snp_makeConstraints { (make) in
            make.left.equalTo(textView.snp_left).offset(5)
            make.top.equalTo(textView.snp_top).offset(8)
        }
    
    }


}

extension DFComposeViewController: UITextViewDelegate {

    func textViewDidChange(textView: UITextView) {
        //发生修改
        placeHolderLabel.hidden = textView.hasText()
        rightBarbuttonItem.enabled = textView.hasText()
    }

}

